//Source file: H:\\temp\\generated\\multidiagram\\EditorFactory.java

package com.cea.papyrus.multidiagram.extension;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.ui.IEditorPart;

/**
 * Simple implementation of IEditorFactory
 * @author dumoulin
 *
 */
public class EditorFactory implements IEditorFactory 
{
   
	/**
	 * Expected Class of the diagram to create.
	 */
	   private Class<IEditorPart> diagramClass;
	   
	   /**
	    * Expected class of the root object.
	    */
	   private Class rootExpectedClass;

	   /**
	    * Constructor.
	    * @param diagramClass
	    * @param rootExpectedClass
	    */
   public EditorFactory(Class<IEditorPart> diagramClass, Class rootExpectedClass) {
		this.diagramClass = diagramClass;
		this.rootExpectedClass = rootExpectedClass;
	}

   /**
    * Dafault implementation return wether the root is eINSTANCE of rootExpectedClass.
    * @param root
    * @return boolean
    */
   public boolean isEditorFor(Object root) 
   {
    return (rootExpectedClass.isInstance(root));
   }
   
   /**
    * Create a new editor for the specified root object.
    * @param sharedObjects
    * @param root
    * @return multidiagram.NestedDiagram
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 * @throws IllegalArgumentException 
    */
   public IEditorPart createEditorFor(MultiDiagramSharedObjects sharedObjects, Object root)  
   {
       IEditorPart editor;
	   
		try {
			Constructor<IEditorPart> c = diagramClass.getConstructor(Object.class, MultiDiagramSharedObjects.class);
			editor = c.newInstance(root, sharedObjects);
			return editor;
			
		} catch (SecurityException e) {
			// Lets propagate. This is an implementation problem that should be solved by programmer.
			throw new RuntimeException(e);
		} catch (NoSuchMethodException e) {
			// Try with no arg constructor
			return (GraphicalEditor)createEditorNoArgs(sharedObjects, root);
		} catch (InstantiationException e) {
			// Lets propagate. This is an implementation problem that should be solved by programmer.
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			// Lets propagate. This is an implementation problem that should be solved by programmer.
			throw new RuntimeException(e);
		} catch (InvocationTargetException e) {
			// Lets propagate. This is an implementation problem that should be solved by programmer.
			throw new RuntimeException(e);
		}
		
   }

   /**
    * Create an editor using its no args constructor. The editor should implements the NestedEdidor interface.
    * @param sharedObjects
    * @param root
    * @return
    * @throws InstantiationException
    * @throws IllegalAccessException
    */
private NestedDiagram createEditorNoArgs(MultiDiagramSharedObjects sharedObjects, Object root)
{
	// TODO Replace with IEditorPart after checking it is possible with Papyrus.
	// Set the shared object only if the NestDiagram interface is implemented.
	NestedDiagram editor;
	// Create editor
	try {
		editor = (NestedDiagram)diagramClass.newInstance();
	} catch (InstantiationException e) {
		// Lets propagate. This is an implementation problem that should be solved by programmer.
		throw new RuntimeException(e);
	} catch (IllegalAccessException e) {
		// Lets propagate. This is an implementation problem that should be solved by programmer.
		throw new RuntimeException(e);
	}
	editor.setSharedObjects(sharedObjects);
	editor.setRoot(root);
	return editor;
}

/**
 * @return the diagramClass
 */
protected Class getDiagramClass() {
	return diagramClass;
}

}
